﻿using IntelligentGateway.Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentGateway.Tools
{
    public class HttpTools
    {
        public static async Task<ResponseMessage> HttpClientPostSyncData<T>(string url, T t, Dictionary<string, string> headers = null)
        {
            string strJson = JsonConvert.SerializeObject(t, new JsonSerializerSettings() { ContractResolver = new CamelCasePropertyNamesContractResolver() });
            HttpContent content = new StringContent(strJson, UnicodeEncoding.UTF8, "application/json");
            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            if (headers != null)
            {
                foreach (var key in headers.Keys)
                {
                    client.DefaultRequestHeaders.Add(key, headers[key]);
                }
            }
            try
            {
                HttpResponseMessage responseMessage = await client.PostAsync(url, content);
                string strContent = await responseMessage.Content.ReadAsStringAsync();
                ResponseMessage o = JsonConvert.DeserializeObject<ResponseMessage>(strContent);
                return o;
            }
            catch (Exception ex)
            {
                return null;
            }

        }
        public static async Task<string> HttpClientPostSyncDataString<T>(string url, T t, Dictionary<string, string> headers = null)
        {
            string strJson = JsonConvert.SerializeObject(t);//, new JsonSerializerSettings() { ContractResolver = new CamelCasePropertyNamesContractResolver() });
            HttpContent content = new StringContent(strJson, UnicodeEncoding.UTF8, "application/json");
            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            if (headers != null)
            {
                foreach (var key in headers.Keys)
                {
                    client.DefaultRequestHeaders.Add(key, headers[key]);
                }
            }
            try
            {
                HttpResponseMessage responseMessage = await client.PostAsync(url, content);
                string strContent = await responseMessage.Content.ReadAsStringAsync();                
                return strContent;
            }
            catch (Exception ex)
            {
                return null;
            }

        }
        public static async Task<ResponseMessage> HttpClientPostSyncData(string url,string strJson, Dictionary<string, string> headers = null)
        {
             HttpContent content = new StringContent(strJson, UnicodeEncoding.UTF8, "application/json");
            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            if (headers != null)
            {
                foreach (var key in headers.Keys)
                {
                    client.DefaultRequestHeaders.Add(key, headers[key]);
                }
            }
            try
            {
                HttpResponseMessage responseMessage = await client.PostAsync(url, content);
                string strContent = await responseMessage.Content.ReadAsStringAsync();
                ResponseMessage o = JsonConvert.DeserializeObject<ResponseMessage>(strContent);
                return o;
            }
            catch (Exception ex)
            {
                return null;
            }
        }       
    }
}
